package com.kedll.library.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.kedll.library.R;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.ViewUtil;
import com.kedll.library.widget.linearlistview.LinearListView;


/**
 * Created by QiQi on 15/12/16.
 */
public class MyTabBar extends LinearListView {
    private int selectPosition = -1;

    private int normalTextColor = 0xff707070;
    private int selectTextColor = 0xff3169da;

    private int markBackground = R.drawable.drawable_default_mark;
    private int markTextColor = Color.WHITE;
    private TabAdapter adapter;
    private OnTabSelectListener tabSelectListener;
    private int itemWidth;

    public MyTabBar(Context context) {
        this(context, null);
    }

    public MyTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(LinearListView parent, View view, int position, long id) {
                setCurrentItem(position);
            }
        });

    }

    public void setCurrentItem(int position) {
        if (selectPosition == position) {
            return;
        }
        selectPosition = position;
        adapter.notifyDataSetChanged();
        if (tabSelectListener != null) {
            tabSelectListener.onSelection(position);
        }
    }

    /**
     * 设置导航栏正常的文字颜色
     *
     * @param normalTextColor
     */
    public void setNormalTextColor(int normalTextColor) {
        this.normalTextColor = normalTextColor;
    }

    /**
     * 设置导航栏被选中的文字颜色
     *
     * @param selectTextColor
     */
    public void setSelectTextColor(int selectTextColor) {
        this.selectTextColor = selectTextColor;
    }

    /**
     * 设置角标的背景
     *
     * @param markBackground
     */
    public void setMarkBackground(int markBackground) {
        this.markBackground = markBackground;
    }

    /**
     * 设置角标字体颜色
     *
     * @param markTextColor
     */
    public void setMarkTextColor(int markTextColor) {
        this.markTextColor = markTextColor;
    }

    /**
     * 设置资源文件,必须设置
     *
     * @param normals  正常的Icon
     * @param presseds 按下的Icon
     * @param names    名称
     */
    public void setResources(int[] normals, int[] presseds, String[] names) {
        if (adapter == null) {
            adapter = new TabAdapter(getContext());
            setAdapter(adapter);
        }
        if (normals == null || presseds == null || names == null) {
            throw new NullPointerException("资源文件不能为空");
        }

        if (normals.length != presseds.length || normals.length != names.length) {
            throw new IllegalArgumentException("资源文件组长度不匹配");
        }

        itemWidth = getResources().getDisplayMetrics().widthPixels / normals.length;

        for (int i = 0; i < normals.length; i++) {
            MenuItemVo itemVo = new MenuItemVo();
            itemVo.normal = normals[i];
            itemVo.pressed = presseds[i];
            itemVo.name = names[i];

            adapter.add(itemVo);
        }
    }

    @Override
    public TabAdapter getAdapter() {
        return adapter;
    }

    public void setOnTabSelectListener(OnTabSelectListener tabSelectListener) {
        this.tabSelectListener = tabSelectListener;
    }

    public final class TabAdapter extends ArrayAdapter<MenuItemVo> {

        public TabAdapter(Context context) {
            super(context, 0);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.home_tab_item, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
                LayoutParams params = new LayoutParams(itemWidth, ViewGroup.LayoutParams.MATCH_PARENT);
                convertView.setLayoutParams(params);
                FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params1.leftMargin = Math.round(itemWidth / 2f);
                params1.topMargin = MyUtils.dp2px(5);
                holder.mark.setLayoutParams(params1);
                holder.mark.setBackgroundResource(markBackground);
                holder.mark.setTextColor(markTextColor);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            MenuItemVo itemVo = getItem(position);
            if (position == selectPosition) {
                holder.text1.setCompoundDrawablesWithIntrinsicBounds(0, itemVo.pressed, 0, 0);
                holder.text1.setTextColor(selectTextColor);
            } else {
                holder.text1.setCompoundDrawablesWithIntrinsicBounds(0, itemVo.normal, 0, 0);
                holder.text1.setTextColor(normalTextColor);
            }
            if (itemVo.count > 0) {
                ViewUtil.setGone(holder.mark, false);
                if (itemVo.count <= 99) {
                    holder.mark.setText(String.valueOf(itemVo.count));
                } else {
                    holder.mark.setText("99+");
                }
            } else {
                ViewUtil.setGone(holder.mark, true);
            }
            holder.text1.setText(itemVo.name);

            return convertView;
        }
    }

    static class ViewHolder {
        TextView text1;
        TextView mark;

        ViewHolder(View view) {
            mark = view.findViewById(R.id.mark);
            text1 = view.findViewById(android.R.id.text1);
        }
    }


    public final class MenuItemVo {
        public int normal;
        public int pressed;
        public String name;
        public int count = 0;
    }

    public interface OnTabSelectListener {
        void onSelection(int position);
    }

}
